﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Create_card : MonoBehaviour {
	public GameObject  prefabCard;
	public Texture[]  cardsTextures;

	private List<Card> cardlists = new List<Card>(); 
	class Card 
	{
		public int number;
		public Texture textureCard;

		public Card(int n, Texture t)
		{
			number= n;
			textureCard= t;
		}
	}


	// Use this for initialization
	void Start () {
		for(int i=0; i<cardsTextures.Length;i++)
		{
			// tao ra 2 the bai giong nhau luu vao coso dulieu
			cardlists.Add(new Card(i,cardsTextures[i]));
			cardlists.Add(new Card(i,cardsTextures[i]));

		}

		if (cardsTextures.Length > 0) {
			RandomCard();
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void RandomCard ()     // random card position
	{
		int Tong = cardlists.Count;

		List <Card> temp = new List<Card> ();
		for(int  i=0 ; i< Tong ; i++){
			int random = Random.Range(0,Tong- i);
			temp.Add (cardlists[random]);
			cardlists.RemoveAt (random);
		}

		cardlists = temp;
		CreateGameObject ();

		for (int  i=0; i< Tong; i++) {
			Debug.Log (cardlists [i].number);
		}

	}

	void CreateGameObject()
	{
		int row = 4;					// so hang
		int colum = cardlists.Count / row;				// so cot
		if (cardlists.Count % row > 0) {
			colum+=1;									// chia du thi cot +1
		}
		 	
		float spaceBetweenCard = .2f;

		for (int i=0; i< cardlists.Count; i++) {
		
			GameObject  card= Instantiate(prefabCard, new Vector3((i%row+(i%row*spaceBetweenCard))-(row/2f)+spaceBetweenCard, 0, (i/row+(i/row*spaceBetweenCard))-(row/2f)+spaceBetweenCard),prefabCard.transform.rotation)  as GameObject ;
			card.GetComponentInChildren<MemoryCards>().SetNumberCards(cardlists[i].textureCard, cardlists[i].number);	
		}



	}
}
